#include "StdAfx.h"
#include "Form1.h"

//using namespace System;



Void Graphics_Test::Form1::button1_Click(System::Object ^sender, System::EventArgs ^e) {
	
	array<String^>^ fieldsName = {"DT","RotatingSpeedPED"};
	//array<String^>^ selection = ReadDbField("Test.his", "history", "RotatingSpeedPED");
	array<array<String^>^>^ selection = ReadDbFields("Test.his", "history", fieldsName);
	
	//System::Windows::Forms::DataVisualization::Charting::Series^ series1;
	//series1 = chart1->Series[0];
	

	System::Windows::Forms::DataVisualization::Charting::Series^  series = chart1->Series[0];
	//series->IsValueShownAsLabel = true;

	//DateTime^ date = System::Convert::ToDateTime("13.01.00 04:47"); 
	//String^ dateS = date->ToString("MMMM dd, yyyy H:mm:ss");

	array<String^>^ X = {"13.01.00 03:47","08.02.00 06:20","12.03.00 15:30","13.01.00 03:47","08.02.00 06:20","12.03.00 15:30"};
	array<String^>^ Y = {"3000","1500","2000","3000","1500","2000"};
	for (int i=0; i < X->Length ; i++)
		series->Points->AddXY( X[i], Y[i]);
	

	/*for (int i=0; i<selection->Length ; i++){
		//chart1->Series[0]->Points->AddY(10);
		//DateTime^ date = System::Convert::ToDateTime(selection[i][0]);
		String^ x = selection[i][0];
		String^ y = selection[i][1];
		series->Points->AddXY( selection[i][0] , selection[i][1]);
		
	}*/





}

//---------------------------------------------------------------------------------------


array<String^>^ Graphics_Test::Form1::ReadDbField(System::String ^ database , System::String^ nameTable, System::String^ fieldName) {
	
	array<String^>^ field;

	String^ sql = "SELECT " + fieldName + " FROM " + nameTable + ";";
	SQLiteConnection^ connection = gcnew SQLiteConnection(String::Format("Data Source={0}; Version=3;", database));
	SQLiteCommand^ command = gcnew SQLiteCommand(sql, connection);
	SQLiteDataReader^ reader; 
	
	connection->Open();
	reader = command->ExecuteReader();
	

	int i = 0;
	
	while (reader->Read())
	{		
		field->Resize( field, i+1);
		field[i] = reader[0]->ToString();
		i++;
	}
	connection->Close();	

	return field;
}

//---------------------------------------------------------------------------------------

array<array<System::String^>^>^ Graphics_Test::Form1::ReadDbFields(System::String ^ database , System::String^ nameTable, array<System::String^>^ fieldsName){
	array<array<String^>^>^ fields;

	String^ sql = "SELECT ";
	for (int i= 0; i < fieldsName->Length-1; i++ ){
		sql += fieldsName[i]->ToString()+",";
	}
	sql += fieldsName[fieldsName->Length-1]->ToString()+" FROM " + nameTable + ";";

	SQLiteConnection^ connection = gcnew SQLiteConnection(String::Format("Data Source={0}; Version=3;", database));
	SQLiteCommand^ command = gcnew SQLiteCommand(sql, connection);
	SQLiteDataReader^ reader; 
	
	connection->Open();
	reader = command->ExecuteReader();
	

	int i = 0;
	
	while (reader->Read())
	{		
		int fieldC = reader->FieldCount;
		array<String^>^ field;
		field->Resize(field,fieldC);

		fields->Resize( fields, i+1);
		for (int j=0; j < fieldC; j++){
			
			field[j] = reader[j]->ToString();
		}
		fields[i] = field;
		i++;
	}
	connection->Close();	

	return fields;
}

//---------------------------------------------------------------------------------------
